package com.heiyu.task;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSON;
import com.heiyu.dao.ClientPushInfoMapper;
import com.heiyu.dao.UserPushInfoMapper;
import com.heiyu.entity.UserPushInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//@Component
public class JPushTask {

    private final static Logger log = LoggerFactory.getLogger(JPushTask.class);

    @Autowired
    private ClientPushInfoMapper clientPushInfoMapper;
    @Autowired
    private UserPushInfoMapper userPushInfoMapper;
    @Scheduled(fixedRate = 5000)
    public void execute(){
        UserPushInfo pushInfo = new UserPushInfo();
        pushInfo.setStatus("1");
        List<UserPushInfo> list = userPushInfoMapper.selectList(pushInfo);
        if(CollectionUtils.isNotEmpty(list)){
            JPushClient jpushClient = new JPushClient("ad3eecc9c7f4cdcfe56e6c06", "e876983a0279cfef065d77f4", null, ClientConfig.getInstance());
            PushResult result;
            for(UserPushInfo info:list){
                try {
                    result = jpushClient.sendPush(pushMessage(info));
                    log.info(JSON.toJSONString(result));
                    info.setStatus("9");
                    this.userPushInfoMapper.updateById(info);
                } catch (APIConnectionException e) {
                    e.printStackTrace();
                } catch (APIRequestException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    private PushPayload pushMessage(UserPushInfo pushInfo){
        String[] userIds = pushInfo.getPushIds().split(",");
        Map<String,String>  map = null;
        if(StringUtils.isNoneBlank(pushInfo.getExt())){
            map = JSON.parseObject(pushInfo.getExt(),Map.class);
        }

        if(pushInfo.getClientType().equals("1")){
            return  PushPayload.newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.registrationId(Arrays.asList(userIds)))
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .setAlert(pushInfo.getTitle()+"\n"+pushInfo.getContent())
                                    .setSound("happy")
                                    .addExtras(map)
                                    .build())
                            .build())
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(true)
                            .build())
                    .build();
        }else{
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.registrationId(userIds)).
                            setNotification(Notification.android(pushInfo.getTitle(), pushInfo.getContent(), map)).build();
        }

    }

}
